<template>
  <button type="primary" @touchend="handleStar">
    {{ star ? '已收藏' : '收藏' }}
  </button>
</template>

<script setup>
import { computed } from 'vue';
import { find, remove } from 'lodash-es'
import { useFavoriteStore } from '../stores/favorite';
const props = defineProps({
  _id: String
})
const favorite = useFavoriteStore()
const star = computed(() => find(favorite.orders, o => o._id === props._id))
function handleStar() {
  if (star.value) {
    uni.request({
      url: "/order/cancel-star/" + props._id,
      success: ({ data }) => {
        remove(favorite.orders, o => o._id === data._id)
        console.log(data)
      }
    })
  } else {
    uni.request({
      url: "/order/star/" + props._id,
      success: ({ data }) => {
        favorite.orders.push(data)
        console.log(data)
      }
    })
  }
}
</script>